﻿using System;
using System.Configuration;

namespace Sustainalytics.LongRunningTaskManager.Interface
{
    /// <summary>
    /// LRTM host config.
    /// </summary>
    public class LongRunningTaskManagerHostConfiguration : ConfigurationSection
    {
        public static class ConfigurationPropertyNames
        {
            public const string DiskFileManagerBaseDirectory = "diskFileManagerBaseDirectory";
            public const string TaskStatusStartIntervalMs = "taskStatusStartIntervalMs";
            public const string TaskStatusUpdateIntervalMs = "taskStatusUpdateIntervalMs";
            public const string UploadTemporaryDirectory = "uploadTemporaryDirectory";
        }

        /// <summary>
        /// Disk file manager base directory.
        /// </summary>
        [ConfigurationProperty(ConfigurationPropertyNames.DiskFileManagerBaseDirectory, IsRequired = true)]
        public string DiskFileManagerBaseDirectory
        {
            get { return (string)this[ConfigurationPropertyNames.DiskFileManagerBaseDirectory]; }
            set { this[ConfigurationPropertyNames.DiskFileManagerBaseDirectory] = value; }
        }

        /// <summary>
        /// SignalR task status start interval in milliseconds.
        /// </summary>
        [ConfigurationProperty(ConfigurationPropertyNames.TaskStatusStartIntervalMs)]
        public float TaskStatusStartIntervalMs
        {
            get { return (float?)this[ConfigurationPropertyNames.TaskStatusStartIntervalMs] ?? 1000.0f; }
            set { this[ConfigurationPropertyNames.TaskStatusStartIntervalMs] = value; }
        }

        public TimeSpan TaskStatusStartInterval
        {
            get
            {
                return TimeSpan.FromMilliseconds(this.TaskStatusStartIntervalMs);
            }
        }

        /// <summary>
        /// SignalR task status update interval in milliseconds.
        /// </summary>
        [ConfigurationProperty(ConfigurationPropertyNames.TaskStatusUpdateIntervalMs)]
        public float TaskStatusUpdateIntervalMs
        {
            get { return (float?)this[ConfigurationPropertyNames.TaskStatusUpdateIntervalMs] ?? 1000.0f; }
            set { this[ConfigurationPropertyNames.TaskStatusUpdateIntervalMs] = value; }
        }

        public TimeSpan TaskStatusUpdateInterval
        {
            get
            {
                return TimeSpan.FromMilliseconds(this.TaskStatusUpdateIntervalMs);
            }
        }

        /// <summary>
        /// Temporary upload directory.
        /// </summary>
        [ConfigurationProperty(ConfigurationPropertyNames.UploadTemporaryDirectory, IsRequired = true)]
        public string UploadTemporaryDirectory
        {
            get { return (string)this[ConfigurationPropertyNames.UploadTemporaryDirectory]; }
            set { this[ConfigurationPropertyNames.UploadTemporaryDirectory] = value; }
        }


        /// <summary>
        /// Get default LRTM configuration
        /// </summary>
        public static LongRunningTaskManagerHostConfiguration Current
        {
            get
            {
                string sectionName = "longRunningTaskManagerHost";

                LongRunningTaskManagerHostConfiguration configSection = GetConfig(sectionName);

                return configSection;
            }
        }

        /// <summary>
        /// Get configuration.
        /// </summary>
        /// <param name="sectionName"> Config. section name. </param>
        /// <returns> LRTM configuration. </returns>
        public static LongRunningTaskManagerHostConfiguration GetConfig(string sectionName)
        {
            LongRunningTaskManagerHostConfiguration configSection = ConfigurationManager.GetSection(sectionName) as LongRunningTaskManagerHostConfiguration;

            if (configSection == null)
            {
                throw new LongRunningTaskManagerHostConfigurationNotFoundException(sectionName);
            }

            return configSection;
        }
    }
}
